/*
 * This file is part of LangMan.
 *
 * LangMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * LangMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LangMan.  If not, see <http://www.gnu.org/licenses/>.
 */

package langman.util.homogenizer;

import com.google.common.base.Charsets;
import langman.business.data.TranslationException;
import langman.util.languagefile.commons.TranslationMode;
import langman.util.languagefile.writer.LanguageWriter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.PropertyResourceBundle;

public class ReferenceBasedHomogenizer implements AutoCloseable {

    private final ReferenceDescription referenceDescription;
    private final BufferedReader sourceReader;
    private final BufferedWriter targetWriter;

    public ReferenceBasedHomogenizer(ReferenceDescription referenceDescription, File sourceFile, File targetFile) throws IOException {
        this(referenceDescription,
                Files.newBufferedReader(sourceFile.toPath(), Charsets.UTF_8),
                Files.newBufferedWriter(targetFile.toPath(), Charsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING));
    }

    public ReferenceBasedHomogenizer(ReferenceDescription referenceDescription, BufferedReader sourceReader, BufferedWriter targetWriter) {
        this.referenceDescription = referenceDescription;
        this.sourceReader = sourceReader;
        this.targetWriter = targetWriter;
    }


    public void writeLanguage() throws TranslationException {
        PropertyResourceBundle languageBundle;
        try {
            languageBundle = new PropertyResourceBundle(sourceReader);
        } catch (IOException e) {
            throw new TranslationException(e);
        }


        try (LanguageWriter writer = new LanguageWriter(targetWriter)) {
            writeLanguage(languageBundle, writer);
        } catch (IOException e) {
            throw new TranslationException(e);
        }
    }

    private void writeLanguage(PropertyResourceBundle languageBundle, LanguageWriter fileWriter) throws IOException {
        for (String category : referenceDescription.getCategories()) {
            writeCategory(languageBundle, fileWriter, category);
        }
    }

    private void writeCategory(PropertyResourceBundle languageBundle, LanguageWriter writer, String category) throws IOException {
        writer.writeCategory(category);
        for (String key : referenceDescription.getKeysByCategory(category)) {
            String translation;
            TranslationMode translationMode = null;
            if (languageBundle.containsKey(key)) {
                translation = languageBundle.getString(key);
                translationMode = referenceDescription.getTranslationModeForKey(key);
                if (translationMode == TranslationMode.AsReference || translationMode == TranslationMode.DontTranslate) {
                    translation = referenceDescription.getReferenceTranslation(key);
                }
            } else {
                translation = referenceDescription.getReferenceTranslation(key);
            }
            writer.writeTranslation(key, translation, translationMode);
        }
    }

    @Override
    public void close() throws Exception {
        targetWriter.close();
        sourceReader.close();
    }
}
